package org.mybatis.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.dao.IUser;
import org.mybatis.model.User;

public class UserDaoImpl implements IUser {
    
    private SqlSessionFactory sqlSessionFactory;
    
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        
        // 使用接口注解时时需注册接口类
        // sqlSessionFactory.getConfiguration().addMapper(IUser.class);
    }
    
    @Override
    public List<User> searchUser() {
        List<User> users = null;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            IUser mapper = sqlSession.getMapper(IUser.class);
            users = mapper.searchUser();
            sqlSession.commit();
        }
        catch (Exception e){
            System.out.println("mapper.searchUser 出错了！！！");
            e.printStackTrace();
        }
        finally{
            sqlSession.close();
        }
        return users;
    }
    
    @Override
    public int insertUser(User user) {
        int result = 0;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            IUser mapper = sqlSession.getMapper(IUser.class);
            result = mapper.insertUser(user);
            sqlSession.commit();
        }
        catch (Exception e){
            System.out.println("mapper.insertUser 出错了！！！");
            e.printStackTrace();
        }
        finally{
            sqlSession.close();
        }
        return result;
    }
    
    /**
     * @deception TODO
     * @author 米mi
     * @date 2017年11月27日下午4:03:25
     */
    public int updateUser(User user) {
        int result = 0;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            IUser mapper = sqlSession.getMapper(IUser.class);
            result = mapper.updateUser(user);
            sqlSession.commit();
        }
        catch (Exception e){
            System.out.println("mapper.updateUser 出错了！！！");
            e.printStackTrace();
        }
        finally{
            sqlSession.close();
        }
        return result;
    }
    
    /**
     * @deception TODO
     * @author 米mi
     * @date 2017年11月27日下午4:10:27
     */
    public User searchUserById(String id) {
        User user = null;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            IUser mapper = sqlSession.getMapper(IUser.class);
            user = mapper.searchUserById(id);
        }
        catch (Exception e){
            System.out.println("mapper.searchUserById 出错了！！！");
            e.printStackTrace();
        }
        finally{
            sqlSession.close();
        }
        return user;
    }
    
    /**
     * @deception TODO
     * @author 米mi
     * @date 2017年11月28日上午11:02:00
     */
    public int insertByList(List<User> userList) {
        int result = 0;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            IUser mapper = sqlSession.getMapper(IUser.class);
            result = mapper.insertByList(userList);
            sqlSession.commit();
        }
        catch (Exception e){
            System.out.println("mapper.insertById 出错了！！！");
            e.printStackTrace();
        }
        finally{
            sqlSession.close();
        }
        return result;
    }
    
    /**
     * @deception TODO
     * @author 米mi
     * @date 2017年11月29日上午2:07:19
     */
    public int deleteByList(List<String> uList) {
        int result = 0;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            IUser mapper = sqlSession.getMapper(IUser.class);
            result = mapper.deleteByList(uList);
            sqlSession.commit();
        }
        catch (Exception e){
            System.out.println("mapper.deleteByList 出错了！！！");
            e.printStackTrace();
        }
        finally{
            sqlSession.close();
        }
        return result;
    }
    
}
